/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.mleiria.bo.mother;

import java.util.List;
import java.util.logging.Level;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;
import pt.mleiria.core.mother.Mother;
import pt.mleiria.core.socket.VoiceClient;
import pt.mleiria.vo.Phrase;
import pt.mleiria.vo.VoiceCmdVO;

/**
 *
 * @author manuel
 */
@Stateless
public class MotherVoiceEJB {

    private static final Logger LOG = Logger.getLogger(MotherVoiceEJB.class);
    private final Mother mother;
    
    public MotherVoiceEJB() {
        mother = new Mother();
    }
    
    public String callMother(final String request) {
        java.util.logging.Logger.getLogger(MotherVoiceEJB.class.getName()).log(Level.INFO, "Request:"+request);
        final String response = mother.sendRequest(request);
        speakMother(VoiceCmdVO.ENG, response);
        return response;
    }

    public void sayHelloMother(final String userName) {
        final VoiceCmdVO voiceCmd = new VoiceCmdVO(VoiceCmdVO.PT, Phrase.getGreeting() + userName);
        send(voiceCmd);
    }
    public void speakMother(final String lang, final String content) {
        final VoiceCmdVO voiceCmd = new VoiceCmdVO(lang, content);
        send(voiceCmd);
    }
    public void speachMother(final List<String> speach){
        final StringBuffer sb = new StringBuffer();
        for(final String str : speach){
            sb.append(str);
            sb.append(";");
        }
        final VoiceCmdVO voiceCmd = new VoiceCmdVO(VoiceCmdVO.PT, sb.toString());
        send(voiceCmd);
    }
    
    private void send(final VoiceCmdVO voiceCmd){
        try {
            final VoiceClient vc = new VoiceClient();
            vc.setClientSocket();
            vc.sendVoiceCommand(voiceCmd.getCommand());
            vc.close();
        } catch (Exception ex) {
            LOG.info("Mother is down..." + ex.getMessage());
        }
    }
    

}
